package main

import (
	"fmt"
	"log"
	"math/rand"
	"time"
)

var c chan int

//var wg sync.WaitGroup

func hello(val int) {
	//defer wg.Done()
	rand.Seed(time.Now().UnixNano())
	sleep := rand.Intn(10)
	time.Sleep(time.Second * time.Duration(sleep))
	c <- sleep
	close(c)
	fmt.Println(val)
}

func proc1(msgCh, quitCh chan int) {
	for i := 0; i < 10000; i++ {
		if i < 5000 {
			msgCh <- i
		} else {
			quitCh <- 0
		}
	}
}

func main() {
	c := make(chan int)

	go func() {
		log.Println("before sleep")
		for i := 0; i < 10; i++ {
			time.Sleep(time.Second)
			log.Println("send value", i)
			c <- i
		}
		close(c)
	}()

	//for s := range(c) {
	//	log.Println("chan s", s)
	//}
	for {
		s, ok := <-c
		if ok {
			log.Println("recieve value", s)
		} else {
			break
		}
	}

	log.Println("Main done wait chan", <-c)
}
